home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Misc / telewords / Source / telenum.c < prev    next >
C/C++ Source or Header  |  1990-05-14  |  3KB  |  117 lines

  1. /*
  2.    Telenum changes each of its string arguments into the corresponding
  3.    telephone number.  There is no restriction on the length of the string
  4.    and input letters can be of either case.
  5.    The mapping used is the standard American mapping, unless the -<digit>
  6.    options are used to change it.  Letters which do not map to any digit
  7.    are printed as themselves.  This is the default for q and z.
  8.   
  9.    Written by James W. Williams
  10.    williams@cs.umd.edu
  11.    Last hacked on 28 January 1990.
  12.       This really ought to be ASNIfied at some point...
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <ctype.h>
  17.  
  18. #define USAGE    fprintf(stderr, "\tusage: telenum [-<digit>c...] ...  word ...\n")
  19.  
  20. /* table is used to map an input letter to the corresponding digit. */
  21. unsigned char    table[256];
  22.  
  23. initTable()
  24. {
  25.     int i;
  26.  
  27.     /* first make each character map to itself, then set up
  28.        default mapping
  29.     */
  30.     for (i=0; i < 256; i++) table[i] = i;
  31.  
  32.     table['A'] = table['a'] = '2';
  33.     table['B'] = table['b'] = '2';
  34.     table['C'] = table['c'] = '2';
  35.     table['D'] = table['d'] = '3';
  36.     table['E'] = table['e'] = '3';
  37.     table['F'] = table['f'] = '3';
  38.     table['G'] = table['g'] = '4';
  39.     table['H'] = table['h'] = '4';
  40.     table['I'] = table['i'] = '4';
  41.     table['J'] = table['j'] = '5';
  42.     table['K'] = table['k'] = '5';
  43.     table['L'] = table['l'] = '5';
  44.     table['M'] = table['m'] = '6';
  45.     table['N'] = table['n'] = '6';
  46.     table['O'] = table['o'] = '6';
  47.     table['P'] = table['p'] = '7';
  48.     table['R'] = table['r'] = '7';
  49.     table['S'] = table['s'] = '7';
  50.     table['T'] = table['t'] = '8';
  51.     table['U'] = table['u'] = '8';
  52.     table['V'] = table['v'] = '8';
  53.     table['W'] = table['w'] = '9';
  54.     table['X'] = table['x'] = '9';
  55.     table['Y'] = table['y'] = '9';
  56. }
  57.  
  58. int
  59. main(argc, argv)
  60. int argc;
  61. char **argv;
  62. {
  63.     int i;
  64.     char *argptr, *cp, optChar, *currentStr;
  65.  
  66.         if (argc < 2) {
  67.         USAGE;
  68.         exit(1);
  69.         }
  70.  
  71.     initTable();
  72.  
  73.     /* process arguments. */
  74.     argv++; argc--;
  75.     while (**argv == '-') {
  76.         argptr = *argv;
  77.         optChar = argptr[1];
  78.         switch (optChar) {
  79.         case '0':
  80.         case '1':
  81.         case '2':
  82.         case '3':
  83.         case '4':
  84.         case '5':
  85.         case '6':
  86.         case '7':
  87.         case '8':
  88.         case '9':
  89.             cp = &argptr[2];
  90.             while (*cp != '\0') {
  91.                 table[*cp] = optChar;
  92.                 if (isupper(*cp))
  93.                     table[tolower(*cp)] = optChar;
  94.                 else if (islower(*cp))
  95.                     table[toupper(*cp)] = optChar;
  96.                 cp++;
  97.             }
  98.             break;
  99.         default: /* unknown option */
  100.             fprintf(stderr, "Unknown option letter %c.\n", argptr[1]);
  101.         }
  102.         argv++; argc--;
  103.     }
  104.  
  105.     if (argc < 1) {
  106.         fprintf(stderr, "Missing argument.\n");
  107.         USAGE;
  108.         exit (1);
  109.     }
  110.  
  111.     while (*argv != NULL) {
  112.         currentStr = *argv++;
  113.         while (*currentStr) putchar(table[*currentStr++]);
  114.         putchar('\n');
  115.     }
  116. }
  117.